package com.way.token.mobile;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

/**
 * 继承自AbstractAuthenticationToken 用于自定义的token
 *
 * @author: 魏彪
 */
public class MobileCodeAuthenticationToken extends AbstractAuthenticationToken {


    // ~ Instance fields
    // ================================================================================================

    /**
     * 认证之前  存的是手机号
     * 认证之后 存的是用户信息
     */
    private final Object principal;
    // private Object credentials;


    public MobileCodeAuthenticationToken(Object principal) {
        super(null);
        this.principal = principal;
        // this.credentials = credentials;
        //设置没有认证
        setAuthenticated(false);
    }


    public MobileCodeAuthenticationToken(Object principal,
                                         Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.principal = principal;
        // this.credentials = credentials;
        // must use super, as we override
        //设置已经认证
        super.setAuthenticated(true);
    }

    @Override
    public Object getCredentials() {
        return null;
    }

    @Override
    public Object getPrincipal() {
        return this.principal;
    }

    @Override
    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
        if (isAuthenticated) {
            throw new IllegalArgumentException(
                    "Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
        }

        super.setAuthenticated(false);
    }

    @Override
    public void eraseCredentials() {
        super.eraseCredentials();
    }
}
